package cn.xhy.module.purchase.controller.admin.materialdemand;

import cn.xhy.module.purchase.dal.dataobject.materialdemand.ProjectOrderDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.xhy.module.purchase.controller.admin.materialdemand.vo.*;
import cn.xhy.module.purchase.dal.dataobject.materialdemand.MaterialDemandDO;
import cn.xhy.module.purchase.dal.dataobject.materialdemand.OfficeOrderDO;

import cn.xhy.module.purchase.service.materialdemand.MaterialDemandService;

@Tag(name = "管理后台 - 单据")
@RestController
@RequestMapping("/purchase/material-demand")
@Validated
public class MaterialDemandController {

    @Resource
    private MaterialDemandService materialDemandService;

    @PutMapping("/submit")
    @Operation(summary = "提交物资申请单审批")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:update')")
    public CommonResult<Boolean> submitContract(@RequestParam("id") Long id) {
        materialDemandService.submitReceivable(id, getLoginUserId());
        return success(true);
    }
    @PostMapping("/create")
    @Operation(summary = "创建单据")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:create')")
    public CommonResult<Long> createMaterialDemand(@Valid @RequestBody MaterialDemandSaveReqVO createReqVO) {
        return success(materialDemandService.createMaterialDemand(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单据")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:update')")
    public CommonResult<Boolean> updateMaterialDemand(@Valid @RequestBody MaterialDemandSaveReqVO updateReqVO) {
        materialDemandService.updateMaterialDemand(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateById")
    @Operation(summary = "更新单据")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:updateById')")
    public CommonResult<Boolean> updateByIdMaterialDemand(@Valid @RequestBody MaterialDemandSaveReqVO updateReqVO) {
        materialDemandService.updateByIdMaterialDemand(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除单据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:delete')")
    public CommonResult<Boolean> deleteMaterialDemand(@RequestParam("id") Long id) {
        materialDemandService.deleteMaterialDemand(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:query')")
    public CommonResult<MaterialDemandRespVO> getMaterialDemand(@RequestParam("id") Long id) {
        MaterialDemandDO materialDemand = materialDemandService.getMaterialDemand(id);
        return success(BeanUtils.toBean(materialDemand, MaterialDemandRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得单据分页")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:query')")
    public CommonResult<PageResult<MaterialDemandRespVO>> getMaterialDemandPage(@Valid MaterialDemandPageReqVO pageReqVO) {
        PageResult<MaterialDemandDO> pageResult = materialDemandService.getMaterialDemandPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialDemandRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单据 Excel")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialDemandExcel(@Valid MaterialDemandPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialDemandDO> list = materialDemandService.getMaterialDemandPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单据.xls", "数据", MaterialDemandRespVO.class,
                        BeanUtils.toBean(list, MaterialDemandRespVO.class));
    }

    // ==================== 子表（办公申请单） ====================

    @GetMapping("/office-order/list-by-document-number")
    @Operation(summary = "获得办公申请单列表")
    @Parameter(name = "documentNumber", description = "单据编号")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:query')")
    public CommonResult<List<OfficeOrderDO>> getOfficeOrderListByDocumentNumber(@RequestParam("documentNumber") String documentNumber) {
        return success(materialDemandService.getOfficeOrderListByDocumentNumber(documentNumber));
    }

    // ==================== 子表（项目物资申请单） ====================

    @GetMapping("/project-order/list-by-document-number")
    @Operation(summary = "获得项目物资申请单列表")
    @Parameter(name = "documentNumber", description = "单据编号")
    @PreAuthorize("@ss.hasPermission('purchase:material-demand:query')")
    public CommonResult<List<ProjectOrderDO>> getProjectOrderListByDocumentNumber(@RequestParam("documentNumber") String documentNumber) {
        return success(materialDemandService.getProjectOrderListByDocumentNumber(documentNumber));
    }

}
